package top.hugo.generator.service;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import top.hugo.domain.TableDataInfo;
import top.hugo.generator.entity.TLimitBoard;
import top.hugo.generator.mapper.TLimitBoardMapper;
import top.hugo.generator.query.TLimitBoardQuery;
import top.hugo.generator.vo.TLimitBoardVo;
import top.hugo.monitor.entity.SysLogininfor;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

/**
 * 集合竞价数据 服务层处理
 *
 * @author kuanghua
 */
@RequiredArgsConstructor
@Service
public class TLimitBoardService {
    private final TLimitBoardMapper tLimitBoardMapper;

    public TableDataInfo<TLimitBoardVo> selectPageTLimitBoardList(TLimitBoardQuery tLimitBoardQuery) {
        LambdaQueryWrapper<TLimitBoard> lqw = getQueryWrapper(tLimitBoardQuery);
        IPage<TLimitBoardVo> page = tLimitBoardMapper.selectVoPage(tLimitBoardQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    public List<TLimitBoard> selectPageTLimitBoardAll(TLimitBoardQuery tLimitBoardQuery) {
        LambdaQueryWrapper<TLimitBoard> lqw = getQueryWrapper(tLimitBoardQuery);
        List<TLimitBoard> tLimitBoards = tLimitBoardMapper.selectList(lqw);
        return tLimitBoards;
    }

    /**
     * 查询集合竞价数据集合
     *
     * @param tLimitBoardQuery 集合竞价数据
     * @return 集合竞价数据集合
     */

    public List<TLimitBoardVo> selectTLimitBoardList(TLimitBoardQuery tLimitBoardQuery) {
        LambdaQueryWrapper<TLimitBoard> lqw = getQueryWrapper(tLimitBoardQuery);
        return tLimitBoardMapper.selectVoList(lqw);
    }


    /**
     * 查询wrapper封装
     *
     * @return
     */
    private static LambdaQueryWrapper<TLimitBoard> getQueryWrapper(TLimitBoardQuery tLimitBoardQuery) {
        LambdaQueryWrapper<TLimitBoard> lqw = new LambdaQueryWrapper<TLimitBoard>();
        lqw.like(ObjectUtil.isNotEmpty(tLimitBoardQuery.getId()), TLimitBoard::getId, tLimitBoardQuery.getId());
        lqw.eq(ObjectUtil.isNotEmpty(tLimitBoardQuery.getName()), TLimitBoard::getName, tLimitBoardQuery.getName());
        lqw.like(ObjectUtil.isNotEmpty(tLimitBoardQuery.getRecordData()), TLimitBoard::getRecordData, tLimitBoardQuery.getRecordData());
        lqw.like(ObjectUtil.isNotEmpty(tLimitBoardQuery.getCreateTime()), TLimitBoard::getCreateTime, tLimitBoardQuery.getCreateTime());
        lqw.like(ObjectUtil.isNotEmpty(tLimitBoardQuery.getUpdateTime()), TLimitBoard::getUpdateTime, tLimitBoardQuery.getUpdateTime());
        lqw.like(ObjectUtil.isNotEmpty(tLimitBoardQuery.getCreateBy()), TLimitBoard::getCreateBy, tLimitBoardQuery.getCreateBy());
        lqw.like(ObjectUtil.isNotEmpty(tLimitBoardQuery.getUpdateBy()), TLimitBoard::getUpdateBy, tLimitBoardQuery.getUpdateBy());


        // 创建时间
        //时间范围
        lqw.between(ObjectUtil.isNotEmpty(tLimitBoardQuery.getBeginTime()) && ObjectUtil.isNotEmpty(tLimitBoardQuery.getEndTime()),
                TLimitBoard::getCreateTime, tLimitBoardQuery.getBeginTime(), tLimitBoardQuery.getEndTime());
        return lqw;
    }

    /**
     * 查询所有平台
     *
     * @return 平台列表
     */
    public List<TLimitBoardVo> selectTLimitBoardAll() {
        return tLimitBoardMapper.selectVoList();
    }

    /**
     * 通过平台ID查询集合竞价数据
     *
     * @param tLimitBoardId 平台ID
     * @return 角色对象信息
     */

    public TLimitBoard selectTLimitBoardById(Long tLimitBoardId) {
        return tLimitBoardMapper.selectById(tLimitBoardId);
    }


    /**
     * 删除集合竞价数据
     *
     * @param tLimitBoardId 平台ID
     * @return 结果
     */

    public int deleteTLimitBoardById(Long tLimitBoardId) {
        return tLimitBoardMapper.deleteById(tLimitBoardId);
    }



    /*根据日期删除数据*/
    public int deleteTLimitBoardByDate(String beginTime,String endTime) {
        LambdaQueryWrapper<TLimitBoard> lqw = new LambdaQueryWrapper<TLimitBoard>();
        // 创建时间
        if (ObjectUtil.isNotEmpty(beginTime) && ObjectUtil.isNotEmpty(endTime)) {
            lqw.between(TLimitBoard::getCreateTime, beginTime,endTime);
        }else{
            throw new RuntimeException("日期不能为空");
        }
        return tLimitBoardMapper.delete(lqw);
    }


    /**
     * 批量删除集合竞价数据
     *
     * @param tLimitBoardIds 需要删除的平台ID
     * @return 结果
     */
    public int deleteTLimitBoardByIds(Long[] tLimitBoardIds) {
        return tLimitBoardMapper.deleteBatchIds(Arrays.asList(tLimitBoardIds));
    }

    /**
     * 新增保存集合竞价数据
     *
     * @param tLimitBoard 集合竞价数据
     * @return 结果
     */

    public int insertTLimitBoard(TLimitBoard tLimitBoard) {
        return tLimitBoardMapper.insert(tLimitBoard);
    }


    /**
     * 修改保存集合竞价数据
     *
     * @param tLimitBoard 集合竞价数据
     * @return 结果
     */

    public int updateTLimitBoard(TLimitBoard tLimitBoard) {
        return tLimitBoardMapper.updateById(tLimitBoard);
    }

}
